package com.lee.treasure.component.redis.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author Lee
 * @date 2020/8/6
 */
@Configuration
@EnableCaching
public class TokenRedisConfig extends BaseRedisConfig {

    @Value("${spring.redis-token.database}")
    private int dbIndex;

    @Value("${spring.redis-token.host}")
    private String host;

    @Value("${spring.redis-token.port}")
    private int port;

    @Value("${spring.redis-token.password}")
    private String password;

    /**
     * 配置redis连接工厂
     */
    @Bean
    public RedisConnectionFactory tokenRedisConnectionFactory() {

        return createConnectionFactory(dbIndex, host, port, password);
    }

    /**
     * 配置redisTemplate 注入方式使用@Resource(name="") 方式注入
     */
    @Bean(name = "tokenRedisTemplate")
    public RedisTemplate<String, String> tokenRedisTemplate() {

        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();

        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(tokenRedisConnectionFactory());
        redisTemplate.afterPropertiesSet();

        return redisTemplate;
    }

}
